package com.alibaba.nacos.console.security;

import com.alibaba.nacos.common.utils.Objects;

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 用户登陆统计信息
 *
 * @author luojing
 * @date 2020/09/15
 */
public class AuthenticationStatus implements Serializable {

    private static final long serialVersionUID = 1021276149087730182L;

    /**
     * 最大允许登陆5次失败。否则禁止登陆
     */
    public static final long MAX_LOGIN_COUNT = 5L;

    /**
     * 失败之后重新开启登陆的时限
     */
    public static final int LOGIN_FAIL_WAIT_MINUTE = 30;

    /**
     * 首次登陆失败时间
     */
    Long firstFailTime;

    /**
     * 最近一次登陆失败时间
     */
    Long lastFailTime;

    /**
     * 失败次数
     */
    AtomicInteger failCount = new AtomicInteger(0);

    /**
     * 登陆名
     */
    String loingName;

    public Long getFirstFailTime() {
        return Objects.isNull(firstFailTime) ? 0L : firstFailTime;
    }

    public void setFirstFailTime(Long firstFailTime) {
        this.firstFailTime = firstFailTime;
    }

    public Long getLastFailTime() {
        return lastFailTime;
    }

    public void setLastFailTime(Long lastFailTime) {
        this.lastFailTime = lastFailTime;
    }

    public void loginFail() {
        failCount.incrementAndGet();
    }

    public void resetLogFail() {
        failCount.set(0);
    }

    public int getFailCount() {
        return failCount.get();
    }

    public String getLoingName() {
        return loingName;
    }

    public void setLoingName(String loingName) {
        this.loingName = loingName;
    }
}
